<?php
/**
 * Class TyModule_Mis_Hook
 * mis app 记录操作记录hook
 */

class Module_Uaction_Hook
{
    /**
     * @param $actionData
     * 推送操作记录hook
     */
    public static function onActionData($actionData){
        if (empty($actionData)) {
            Func_Log::write('misUactionRecord', '数据为空');
            return false;
        }
        // 数据入库
        $actionData['action_ip'] = self::getip();
        $res = Module_Uaction_UactionRecord::getInstance()->addData($actionData);
        if (!$res) {
            Func_Log::write('misUactionRecord', '入库失败,data:' . json_encode($actionData));
        }
        return true;
    }

    public static function getip(){
        $unknown = 'unknown';
        $ip = '';
        if(isset($_SERVER['HTTP_X_CONNECTING_IP']) && trim($_SERVER['HTTP_X_CONNECTING_IP']) != false){
            $ip = trim($_SERVER['HTTP_X_CONNECTING_IP']);
        }else{
            if(!empty($_SERVER['HTTP_X_REAL_FORWARDED_FOR'])){
                $ip = $_SERVER['HTTP_X_REAL_FORWARDED_FOR'];
            }else{
                if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
                    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                    //处理多层代理的情况
                    if(false !== strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ',')){
                        if (false !== strpos($ip, ',')) $ip = reset(explode(',', $ip));
                        $ip = trim($ip);
                    }
                }elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)){
                    $ip = $_SERVER['REMOTE_ADDR'];
                }
            }
        }
        return $ip;
    }
}
